package info.javafans.dao;

import info.javafans.database.DatabaseManager;
import info.javafans.domain.Comment;
import info.javafans.utils.DateTime;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class CommentDao {
	DatabaseManager dbManager = new DatabaseManager();
	
	
	/**
	 * 查找某篇文章所有评论
	 * @return
	 */
	public List<Comment> selectCommentsOfPost(String id) {

		List<Comment> list = new ArrayList<Comment>();
		Connection conn = dbManager.getConnection();
		try {
			Statement stmt = conn.createStatement();
			String sql = "select * from jb_comments where post_id="+id;
			ResultSet result = stmt.executeQuery(sql);

			while (result.next()) {
				Comment comment = new Comment();
				
				comment.setId(""+result.getInt("id"));
				comment.setPostId(""+result.getInt("post_id"));
				comment.setAuthor(result.getString("author"));
				comment.setEmail(result.getString("email"));
				DateTime date = DateTime.format(result.getString("date"));
				comment.setDate(date);
				comment.setContent(result.getString("content"));
				
				list.add(comment);
			}

		} catch (SQLException e) {
			System.out
					.println("info.javafans: CommentDao-selectCommentsOfPost-execute query");
			// e.printStackTrace();
		} finally {
			try {
				conn.close();
				return list;
			} catch (SQLException e) {
				System.out
						.println("info.javafans:  CommentDao-selectCommentsOfPost");
				// e.printStackTrace();
			}
		}

		return null;
	}
	
	/**
	 * 查找最新评论内容
	 * @return
	 */
	public List<Comment> selectNewComments() {

		List<Comment> list = new ArrayList<Comment>();
		Connection conn = dbManager.getConnection();
		try {
			Statement stmt = conn.createStatement();
			String sql = "select * from jb_comments order by date desc limit 0,5";
			ResultSet result = stmt.executeQuery(sql);

			while (result.next()) {
				Comment comment = new Comment();
				
				comment.setId(""+result.getInt("id"));
				comment.setPostId(""+result.getInt("post_id"));
				comment.setAuthor(result.getString("author"));
				comment.setEmail(result.getString("email"));
				DateTime date = DateTime.format(result.getString("date"));
				comment.setDate(date);
				comment.setContent(result.getString("content"));
				
				list.add(comment);
			}

		} catch (SQLException e) {
			System.out
					.println("info.javafans: CommentDao-selectNewComments-execute query");
			// e.printStackTrace();
		} finally {
			try {
				conn.close();
				return list;
			} catch (SQLException e) {
				System.out
						.println("info.javafans:  CommentDao-selectNewComments");
				// e.printStackTrace();
			}
		}

		return null;
	}
	/**
	 * 添加评论
	 * @param comment
	 * @return
	 */
	public boolean addComment(Comment comment) {
		Connection conn = dbManager.getConnection();
		try {
			Statement stmt = conn.createStatement();
			String sql = "insert into jb_comments(post_id,author,email,date,content) values("+comment.getPostId()+
				",'"+comment.getAuthor()+"','"+comment.getEmail()+"','"+comment.getDate().getFmtDateTime()+
				"','"+comment.getContent()+"')";
			if (stmt.executeUpdate(sql) == 1) {
				new PostDao().doCommentCountUp(comment.getPostId());
				return true;
			}
			else {
				return false;
			}			
		} catch (SQLException e) {
			System.out
					.println("info.javafans: CommentDao-addComment-execute query");
			// e.printStackTrace();
		} finally {
			try {
				if (conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				System.out
						.println("info.javafans:  CommentDao-addComment");
				// e.printStackTrace();
			}
		}

		return false;
	}
}
